package com.wc.alorithm_luogu.P1241;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * @Author congge
 * @Date 2023/5/16 16:20
 * @description https://www.luogu.com.cn/problem/P1241
 * 括号序列
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String s = sc.nextLine();

        int n = s.length();
        // 存储当前字符串是否已经匹配
        boolean[] match = new boolean[n];

        char[] chs = s.toCharArray();
        // 将s的所有字符串存入
        LinkedList<Character> ansChars = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            ansChars.add(s.charAt(i));
            if (chs[i] == ')') {
                int idx = i - 1;
                while (idx >= 0) {
                    if (match[idx]){
                        idx--;
                        continue;
                    }
                    if (chs[idx] == '(') {
                        match[idx] = true;
                        match[i] = true;
                        break;
                    } else if (chs[idx] == '[') {
                        break;
                    }
                    idx--;
                }
            } else if (chs[i] == ']') {
                int idx = i - 1;
                while (idx >= 0) {
                    if (match[idx]){
                        idx--;
                        continue;
                    }

                    if (chs[idx] == '[') {
                        match[idx] = true;
                        match[i] = true;
                        break;
                    } else if (chs[idx] == '(') {
                        break;
                    }
                    idx--;
                }
            }
        }

        System.out.println(Arrays.toString(match));
        int offset = 0;

        for (int i = 0; i < n; i++) {
            if (!match[i]) {
                if (chs[i] == ')') {
                    ansChars.add(i + offset, '(');
                } else if (chs[i] == '(') {
                    if (i + offset + 1 == ansChars.size()) {
                        ansChars.add(')');
                    } else {
                        ansChars.add(i + offset + 1, ')');
                    }
                } else if (chs[i] == ']') {
                    ansChars.add(i + offset, '[');
                } else if (chs[i] == '[') {
                    if (i + offset + 1 == ansChars.size()) {
                        ansChars.add(']');
                    } else {
                        ansChars.add(i + offset + 1, ']');
                    }
                }
                offset++;
            }
        }

        StringBuilder ans = new StringBuilder();

        for (char ch : ansChars) {
            ans.append(ch);
        }

        System.out.println(ans);
    }
}
